#include <iostream>
#include <pthread.h>
#include <signal.h>
#include <unistd.h>

static void on_signal_term(int sig)
{
    std::cout << "sub thread will exit" << std::endl;
    pthread_exit(NULL);
}

void *thfunc(void *arg)
{
    signal(SIGQUIT, on_signal_term);

    int tm = 50;
    while (1)
    {
        std::cout << "th left:" << tm << "s--" << std::endl;
        sleep(1);
        tm--;
    }
    return (void *)0;
}

int main()
{
    pthread_t pid;
    int res;
    res = pthread_create(&pid, NULL, thfunc, NULL);

    sleep(5);

    pthread_kill(pid, SIGQUIT);

    pthread_join(pid, NULL);

    std::cout << "sub thread done,main thread exit" << std::endl;
    return 0;
}